package cn.kgc.crowdfundingadminspringboot2.service;

import cn.kgc.crowdfundingadminspringboot2.mapper.AdvertisementMapper;
import cn.kgc.crowdfundingadminspringboot2.pojo.Advertisement;
import cn.kgc.crowdfundingadminspringboot2.pojo.AdvertisementExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AdvertisementServiceimpl implements AdvertisementService{
    @Resource
    AdvertisementMapper advertisementMapper;

    @Override
    public PageInfo<Advertisement> queryAll(Integer pageNum, Integer PageSize, String keyword) {
        PageHelper.startPage(pageNum,PageSize);
        AdvertisementExample example=new AdvertisementExample();
        AdvertisementExample.Criteria criteria=example.createCriteria();
        criteria.andNameLike("%"+keyword+"%");
        List<Advertisement> advertisements = advertisementMapper.selectByExample(example);
        PageInfo<Advertisement> info=new PageInfo<>(advertisements);
        return info;
    }

    @Override
    public void saveAdvertisement(Advertisement advertisement) {

       advertisementMapper.insert(advertisement);
    }

    @Override
    public void batchRemove(List<Integer> advertisementList) {

        AdvertisementExample example=new AdvertisementExample();
        AdvertisementExample.Criteria criteria=example.createCriteria();
        criteria.andIdIn(advertisementList);
        advertisementMapper.deleteByExample(example);
    }

    @Override
    public Advertisement getAdminById(Integer advertisementId) {
        Advertisement advertisement = advertisementMapper.selectByPrimaryKey(advertisementId);
        return advertisement;
    }

    @Override
    public void updateAdmin(Advertisement advertisement) {
        advertisementMapper.updateByPrimaryKeySelective(advertisement);
    }
}
